package com.yamaha.bikemarket;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.PauseOnScrollListener;
import com.yamaha.bikemarket.util.LogUtil;

public class WantToBuyImageGalleryFragment extends AMyActivity {

	private ImageLoader imageLoader;
	private LayoutInflater inflater;

	private MyAdapter adapter;
	private final List<JSONObject> list = new ArrayList<JSONObject>();

	private final String TAG = "WantToBuyImageGalleryFragment";

	public WantToBuyImageGalleryFragment() {
		super(MenuType.NONE);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.wtb_detail_layout);
		inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);

		final DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
				.cacheInMemory().cacheOnDisc().build();
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
				getApplicationContext()).defaultDisplayImageOptions(
				defaultOptions).build();
		if (null != imageLoader && imageLoader.isInited()) {
			imageLoader.destroy();
			imageLoader = null;
		}
		imageLoader = ImageLoader.getInstance();
		if (!imageLoader.isInited()) {
			imageLoader.init(config);
		}

		setListenerForMenu();
	}

	@Override
	protected List<EditText> getEditTexts() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onClick(View v) {
		if (onMenuClick(v)) {
			return;
		}
	}

	@Override
	protected void onResume() {
		super.onResume();

		adapter = new MyAdapter();

		final ListView listView = (ListView) findViewById(R.id.listview);
		listView.setAdapter(adapter);

		boolean pauseOnScroll = true; // or true
		boolean pauseOnFling = true; // or false
		PauseOnScrollListener listener = new PauseOnScrollListener(imageLoader,
				pauseOnScroll, pauseOnFling);
		listView.setOnScrollListener(listener);

		adapter.setLoading(true);

		try {
			// final JSONObject data = SCStaticFunc.getData();

			final String detail = getIntent().getStringExtra("detail");
			final JSONObject item = new JSONObject(detail);

			Log.d(TAG, item.toString());

			list.clear();
			final JSONArray pix = item.getJSONArray("pix");
			Log.d(TAG, "Number of Pix : " + pix.length());
			for (int i = 0; i < pix.length(); i++) {
				list.add(pix.getJSONObject(i));
			}

			adapter.setLoading(false);
			adapter.notifyDataSetChanged();

		} catch (JSONException e) {

			LogUtil.e(TAG, e.getMessage(), e);
			Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
		}
	}

	private static class ItemHolder {
		ImageView image1;
		ImageView image2;
		ImageView image3;
	}

	private final class MyAdapter extends BaseAdapter implements
			View.OnClickListener {
		private boolean isLoading = true;

		public void setLoading(boolean isLoading) {
			this.isLoading = isLoading;
		}

		@Override
		public int getCount() {
			return (isLoading) ? 1 : (int) Math.ceil(list.size() / 3.0);
		}

		@Override
		public Object getItem(int position) {
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (isLoading) {
				convertView = inflater.inflate(R.layout.loading_item, parent,
						false);
				convertView.setId(0x0123);
				return convertView;
			}

			if (convertView != null && convertView.getId() == 0x0123) {
				convertView = null;
			}

			ItemHolder holder;
			if (null == convertView) {
				convertView = inflater.inflate(R.layout.wtb_detail_item,
						parent, false);
				convertView.setId(position);

				holder = new ItemHolder();
				holder.image1 = (ImageView) convertView
						.findViewById(R.id.wtb_image1);
				holder.image2 = (ImageView) convertView
						.findViewById(R.id.wtb_image2);
				holder.image3 = (ImageView) convertView
						.findViewById(R.id.wtb_image3);

				convertView.setTag(holder);

			} else {
				holder = (ItemHolder) convertView.getTag();
			}

			try {

				int imageIndex = position * 3;
				int imageIndex2 = position * 3 + 1;
				int imageIndex3 = position * 3 + 2;

				if (imageIndex < list.size()) {
					final JSONObject obj = list.get(imageIndex);

					final JSONArray pixItem = obj.getJSONArray("pix");
					if (pixItem != null && pixItem.length() > 0) {

						final String url = pixItem.getString(0);
						if (url != null && url.length() > 0) {
							imageLoader.displayImage(url, holder.image1);
						}
					}
					holder.image1.setTag(obj);
				}

				if (imageIndex2 < list.size()) {
					final JSONObject obj = list.get(imageIndex2);

					final JSONArray pixItem = obj.getJSONArray("pix");
					if (pixItem != null && pixItem.length() > 0) {

						final String url = pixItem.getString(0);
						if (url != null && url.length() > 0) {
							imageLoader.displayImage(url, holder.image2);
						}
					}
					holder.image2.setTag(obj);
				}

				if (imageIndex3 < list.size()) {
					final JSONObject obj = list.get(imageIndex3);

					final JSONArray pixItem = obj.getJSONArray("pix");
					if (pixItem != null && pixItem.length() > 0) {

						final String url = pixItem.getString(0);
						if (url != null && url.length() > 0) {
							imageLoader.displayImage(url, holder.image3);
						}
					}
					holder.image3.setTag(obj);
				}

				holder.image1.setOnClickListener(this);
				holder.image2.setOnClickListener(this);
				holder.image3.setOnClickListener(this);

			} catch (JSONException e) {
				Log.e("Want To Buy Galley View", e.getMessage(), e);
			}

			return convertView;
		}

		@Override
		public void onClick(View v) {

			final JSONObject object = (JSONObject) v.getTag();

			if (null != object) {
				LogUtil.i("WantToBuyImageGallery",
						"Item : " + object.toString());

				final Intent intent = new Intent(
						WantToBuyImageGalleryFragment.this,
						BikeDetailFragment.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
				intent.putExtra("item", object.toString());
				startActivity(intent);
			}

		}

	}
}
